package com.arturjaneczko.controller.impl;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import com.arturjaneczko.controller.core.IController;
import com.arturjaneczko.model.core.IModel;
import com.arturjaneczko.services.core.state.IClipboardMode;
import com.arturjaneczko.services.impl.state.FifoClipboardMode;
import com.arturjaneczko.services.impl.state.GrowthClipboardMode;
import com.arturjaneczko.services.impl.state.LifoClipboardMode;
import com.arturjaneczko.services.impl.state.OperatingClipboardMode;
import com.arturjaneczko.services.impl.state.StdClipboardMode;
import com.arturjaneczko.view.core.IView;
import com.arturjaneczko.view.impl.View;
import com.arturjaneczko.view.impl.clipboard.action.PanelCopy;
import com.arturjaneczko.view.impl.clipboard.action.PanelEast;
import com.arturjaneczko.view.impl.clipboard.action.PanelMulti;
import com.arturjaneczko.view.impl.clipboard.action.PanelOff;
import com.arturjaneczko.view.impl.clipboard.action.PanelPaste;
import com.arturjaneczko.view.impl.clipboard.mode.PanelWest;
import com.arturjaneczko.view.impl.clipboard.mode.fifo.PanelFifo;
import com.arturjaneczko.view.impl.clipboard.mode.growth.PanelGrowth;
import com.arturjaneczko.view.impl.clipboard.mode.lifo.PanelLifo;
import com.arturjaneczko.view.impl.clipboard.mode.std.PanelStd;
import com.arturjaneczko.view.impl.clipboard.preview.ClipboardView;
import com.arturjaneczko.view.impl.clipboard.preview.IconView;
import com.arturjaneczko.view.impl.clipboard.preview.PanelCenter;
import com.arturjaneczko.view.impl.clipboard.preview.PanelPreview;
import com.arturjaneczko.view.impl.clipboard.preview.TypeView;

public class Controller implements IController {

	private IModel model;
	private IView view;
	private static final OperatingClipboardMode ocm = new OperatingClipboardMode();
	private static final IClipboardMode MULTI = ocm.getMultiClipboardMode();
	private static final IClipboardMode FIFO = ocm.getFifoClipboardMode();
	private static final IClipboardMode LIFO = ocm.getLifoClipboardMode();
	private static final IClipboardMode GROWTH = ocm.getGrowthClipboardMode();
	private static final IClipboardMode STD = ocm.getStdClipboardMode();
	private static final IClipboardMode OFF = ocm.getOffClipboardMode();

	private OperatingClipboardMode operatingClipboardMode;

	public Controller(OperatingClipboardMode operatingClipboardMode) {
		this.operatingClipboardMode = operatingClipboardMode;
	}

	@Override
	public void setModel(IModel model) {
		this.model = model;
	}

	@Override
	public void setView(IView view) {
		this.view = view;
	}

	private void buildComposite() {
		view = new View(model);
		PanelWest panelWest = new PanelWest(model);
		PanelCenter panelCenter = new PanelCenter(model);
		PanelEast panelEast = new PanelEast(model);

		PanelFifo panelFifo = new PanelFifo(model);
		PanelGrowth panelGrowth = new PanelGrowth(model);
		PanelLifo panelLifo = new PanelLifo(model);
		PanelStd panelStd = new PanelStd(model);

		PanelPreview panelPreview = new PanelPreview(model);
		
		PanelCopy panelCopy = new PanelCopy(model);
		PanelMulti panelMulti = new PanelMulti(model);
		PanelOff panelOff = new PanelOff(model);
		PanelPaste panelPaste = new PanelPaste(model);
		
			ClipboardView cv1 = new ClipboardView(model);
			TypeView tv1 = new TypeView(model);
			IconView iv1 = new IconView(model);
			ClipboardView cv2 = new ClipboardView(model);
			TypeView tv2 = new TypeView(model);
			IconView iv2 = new IconView(model);
			ClipboardView cv3 = new ClipboardView(model);
			TypeView tv3 = new TypeView(model);
			IconView iv3 = new IconView(model);

		panelWest.addComposite(panelStd);
		panelWest.addComposite(panelFifo);
		panelWest.addComposite(panelLifo);
		panelWest.addComposite(panelGrowth);

		panelCenter.addComposite(panelPreview);
		
			panelPreview.addComposite(cv1);
			cv1.addComposite(tv1);
			cv1.addComposite(iv1);
			panelPreview.addComposite(cv2);
			cv2.addComposite(tv2);
			cv2.addComposite(iv2);
			panelPreview.addComposite(cv3);
			cv3.addComposite(tv3);
			cv3.addComposite(iv3);

		panelEast.addComposite(panelMulti);
		panelEast.addComposite(panelOff);
		panelEast.addComposite(panelCopy);
		panelEast.addComposite(panelPaste);

		view.addComposite(panelWest);
		view.addComposite(panelCenter);
		view.addComposite(panelEast);

		view.ui();
		bindListeners();
	}

	private void bindListeners() {
		bindFifoPanel();
		bindGrowthPanel();
		bindLifoPanel();
		bindOffPanel();
		bindMultiPanel();
		bindStdPanel();
	}

	private void bindFifoPanel() {
		view.getFifoPanel().addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				operatingClipboardMode.setClipboardMode(operatingClipboardMode.getClipboardMode() instanceof FifoClipboardMode ? OFF : FIFO);
			}

			@Override
			public void mouseExited(MouseEvent e) {
			}

			@Override
			public void mouseEntered(MouseEvent e) {
			}
		});
	}

	private void bindGrowthPanel() {
		view.getGrowthPanel().addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				operatingClipboardMode.setClipboardMode(operatingClipboardMode.getClipboardMode() instanceof GrowthClipboardMode ? OFF : GROWTH);
			}

			@Override
			public void mouseExited(MouseEvent e) {
			}

			@Override
			public void mouseEntered(MouseEvent e) {
			}
		});
	}

	private void bindLifoPanel() {
		view.getLifoPanel().addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				operatingClipboardMode.setClipboardMode(operatingClipboardMode.getClipboardMode() instanceof LifoClipboardMode ? OFF : LIFO);
			}

			@Override
			public void mouseExited(MouseEvent e) {
			}

			@Override
			public void mouseEntered(MouseEvent e) {
			}
		});
	}
	
	private void bindMultiPanel() {
		view.getMultiPanel().addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				operatingClipboardMode.setClipboardMode(MULTI);
			}

			@Override
			public void mouseExited(MouseEvent e) {
			}

			@Override
			public void mouseEntered(MouseEvent e) {
			}
		});
	}

	private void bindOffPanel() {
		view.getOffPanel().addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				operatingClipboardMode.setClipboardMode(OFF);
			}

			@Override
			public void mouseExited(MouseEvent e) {
			}

			@Override
			public void mouseEntered(MouseEvent e) {
			}
		});
	}

	private void bindStdPanel() {
		view.getStdPanel().addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				operatingClipboardMode.setClipboardMode(operatingClipboardMode.getClipboardMode() instanceof StdClipboardMode ? OFF : STD);
			}

			@Override
			public void mouseExited(MouseEvent e) {
			}

			@Override
			public void mouseEntered(MouseEvent e) {
			}
		});
	}

	public void start() {
		buildComposite();
	}

}
